package model

type Users struct {
	Id       int
	Username string
	Password string
	Enable   int
}

func (user *Users) Create() (err error) {
	_, err = Db.Exec("insert into users (username,password,enable) values ($1,$2,$3)", user.Username, Encrypt(user.Password), user.Enable)
	return
}

func GetUser(username string) (user Users, err error) {
	err = Db.QueryRowx("select id, username, password, enable from users where username = $1", username).StructScan(&user)
	if err != nil {
		return
	}
	return
}

func ListUsers() (users []Users, err error) {
	err = Db.Select(&users, "select id,username,password,enable from users")
	if err != nil {
		return
	}
	return
}

func (user *Users) UpdatePass(password string) (err error) {
	_, err = Db.Exec("update users set password=$1 where id=$2", Encrypt(user.Password), user.Id)
	return
}
